/* eslint-disable */

import MiniProgram from 'miniprogram-automator/out/MiniProgram'

import { IAnyObject, MiniProgramReport, MiniProgramSendEvent } from '@slardar/sdk-mini-program'

import { matchFuzzily } from './helper'

type RecursivePartial<T> = {
  [P in keyof T]?: T[P] extends (infer U)[]
    ? RecursivePartial<U>[]
    : T[P] extends IAnyObject
    ? RecursivePartial<T[P]>
    : T[P]
}

declare const tt: any
export const getSendStackWithTT = async (miniProgram: MiniProgram) => {
  const res = await miniProgram.evaluate(() => {
    return (tt as any).sendStack
  })
  return res
}

export const getReportStackWithTT = async (miniProgram: MiniProgram) => {
  const res = await miniProgram.evaluate(() => {
    return (tt as any).reportStack as MiniProgramReport
  })
  return res
}

export const matchFuzzilyWithSendStackWithTT = async (
  miniProgram: MiniProgram,
  findObj: Partial<MiniProgramSendEvent>,
) => {
  const stack = await getSendStackWithTT(miniProgram)
  return matchFuzzily(stack, findObj) as MiniProgramSendEvent[]
}

export const findFuzzilyWithSendStack = async (
  miniProgram: MiniProgram,
  findObj: Partial<MiniProgramSendEvent>,
  index = 0,
) => {
  const res = await matchFuzzilyWithSendStackWithTT(miniProgram, findObj)
  return res[index]
}

export const matchFuzzilyWithReportStackWithTT = async (
  miniProgram: MiniProgram,
  findObj: RecursivePartial<MiniProgramReport>,
) => {
  const stack = await getReportStackWithTT(miniProgram)
  return matchFuzzily(stack, findObj) as MiniProgramReport[]
}

export const findFuzzilyWithReportStack = async (
  miniProgram: MiniProgram,
  findObj: RecursivePartial<MiniProgramReport>,
  index = 0,
) => {
  const res = await matchFuzzilyWithReportStackWithTT(miniProgram, findObj)
  return res[index]
}
